package com.example.parallaxlistview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.ListView;

public class ParallaxScrollListView extends ListView{

	private Context mContext;
	private int mMaxYOverscrollDistance;
	private int MAX_Y_OVERSCROLL_DISTANCE;
	//最大高度
	private int mDrawableMaxHeight = -1;
	//初始高度
	private int mImageViewHeight = -1;
	private int mDefaultHeight = 160;
	private ImageView mImageView;
	
	public ParallaxScrollListView(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mContext = context;
		init(context);
	}

	private void init(Context context) {
//		mDefaultHeight = context.getResources().getDimensionPixelSize(R.id.action_settings);
		
	}

	public ParallaxScrollListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public ParallaxScrollListView(Context context) {
		super(context);
		init(context);
	}

	private void initBounceListView(){
		final DisplayMetrics metris  = mContext.getResources().getDisplayMetrics();
		final float density = metris.density;
		mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
	}
	
	// 设置缩放级别
	public void setViewBounds(double zoomRatio) {
		// 图片默认高度
		if (mImageViewHeight == -1) {
			mImageViewHeight = mImageView.getHeight();
			if (mImageViewHeight <= 0) {
				mImageViewHeight = mDefaultHeight;
			}
		}
		//最大可拉伸高度  ImageView的高度
		//drawable的高度/ImageView的高度 = drawable的宽度/ImageView的宽度
		//宽度固定  通过比例推出 ratio = drawable的宽度/ImageView的宽度
		double ratio = ((double)mImageView.getDrawable().getIntrinsicWidth())/((double)mImageView.getWidth());
		double  height = mImageView.getDrawable().getIntrinsicHeight()/ratio;
		mDrawableMaxHeight =  (int) (height*(zoomRatio>1?zoomRatio:1));
	}
	
	//设置图片
	public void setParallaxImageView(ImageView iv){
		mImageView = iv;
		mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
	}
	
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		//滑动的时候回调
		/**
		 * 当下拉放大后不松开，并上滑图片的时候缩小图片
		 */
		//缩小ImageVIew的高度，直至缩小到原来默认ImageView的高度
		View firstView = (View) mImageView.getParent();
		//推出去的高度firstView.getTop();--->小于0
		System.out.println(mImageView.getHeight()+"--->"+mImageViewHeight);
		if( (firstView.getTop() < 0) && (mImageView.getHeight() > mImageViewHeight) ){
			mImageView.getLayoutParams().height = Math.max(mImageView.getHeight()+firstView.getTop(), mImageViewHeight);
			//firstView也要调整高度 firstView.getTop()--->0
			firstView.layout(firstView.getLeft(), 0, firstView.getRight(), firstView.getHeight());
			mImageView.requestLayout();
		}
	}
	
	@Override
	protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
			int scrollY, int scrollRangeX, int scrollRangeY,
			int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
		//当ListView滑动超出范围是回调该方法，并且提供参数给你做一些处理
		//当下啦超出边界时回调
	    //监听下拉，让图片高度不断的增加，上拉减小
		
		boolean x = resizeOverScrollBy(deltaX, deltaY, scrollX,
				 scrollY,  scrollRangeX,  scrollRangeY,
				 maxOverScrollX,  maxOverScrollY,  isTouchEvent);
		
		return x ? x:super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
				scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
	}
	
	public boolean resizeOverScrollBy(int deltaX, int deltaY, int scrollX,
			int scrollY, int scrollRangeX, int scrollRangeY,
			int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent){
		if( mImageView.getHeight() <= mDrawableMaxHeight && isTouchEvent ){
			
		/**
		 * 让图片高度不断的增加减小
		 * 改变ImageView的高度，然后mImageView.requestLayout();从新布局，从新调整大小；
		 * 高度最大放大到mDrawableMaxHeight
		 */
		/**
		 * deltaY
		 * 是在超出滑动的时候每毫秒滑动的距离
		 * (-代表放下拉，+代表往上拉)
		 * 大小是根据实用滑动的快慢决定的。一般的速度范围是-50~50
		 * 
		 */
			if( deltaY <0 ){
				//往下拉增加高度
				mImageView.getLayoutParams().height = mImageView.getHeight()-deltaY;
//				mImageView.getLayoutParams().width = mImageView.getWidth()-deltaY;
				mImageView.requestLayout();
			}else{
				//往上拉减少高度
				mImageView.getLayoutParams().height = mImageView.getHeight()-deltaY > mImageViewHeight ?(mImageView.getHeight()-deltaY):mImageViewHeight;
//				mImageView.getLayoutParams().width = mImageView.getWidth()-deltaY;
				mImageView.requestLayout();
			}
		}
		return false;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		//松开手自动弹上去 
		if( ev.getAction() == MotionEvent.ACTION_UP ){
			//使用动画--duration
			//自定义动画
			ResetAnimation resetAnimation= new ResetAnimation(mImageView, mImageViewHeight);
			resetAnimation.setDuration(300);
			mImageView.startAnimation(resetAnimation);
		}
		
		return super.onTouchEvent(ev);
	}
	
	public class ResetAnimation extends Animation{
		
		private ImageView image;
		private int targetHeight;
		private int originalHeight;
		private int extraHeight;
		
		public ResetAnimation(ImageView mImageView, int targetHeight) {
			this.image = mImageView;
			this.originalHeight = mImageView.getHeight();
			this.targetHeight = targetHeight;
			extraHeight = originalHeight - targetHeight;
		}
		
		@Override
		protected void applyTransformation(float interpolatedTime,
				Transformation t) {
			//interpolatedTime变化范围 0.0 to 1.0 (变化对应于 0~duration ) 
			//差值器
			//高度从originalHeight--->targetHeight
			image.getLayoutParams().height = (int) (originalHeight - extraHeight*interpolatedTime);
			image.requestLayout();
			
			
			super.applyTransformation(interpolatedTime, t);
		}
		
	}
	
}
